﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.Mod.Groups.Events
{
    using SharingCenter.Web.Classes;
    using SharingCenter.Web.Classes.Events;
    public partial class Edit : PageBase
    {
        protected Sc_Event _event = null;

        protected void Page_Init()
        {
            SetNav("events");
            bool shouldContinue = true;
            Guid eid = Guid.NewGuid();
            try
            {
                eid = new Guid(Request.QueryString["id"]);
            }
            catch
            {
                shouldContinue = false;
            }
            _event = EventOperations.GetEventFromEvnetId(eid);
            shouldContinue = _event != null;
           

            if (!shouldContinue)
            {
                RedirectToDefaultPage();
            }
            else
            {
                LinqDataSource1.Selecting += new EventHandler<LinqDataSourceSelectEventArgs>(LinqDataSource1_Selecting);
                //LinqDataSource1.Updating += new EventHandler<LinqDataSourceUpdateEventArgs>(LinqDataSource1_Updating);
                LinqDataSource1.Updated += new EventHandler<LinqDataSourceStatusEventArgs>(LinqDataSource1_Updated);
                FormView1.ItemUpdating += new FormViewUpdateEventHandler(FormView1_ItemUpdating);
            }
        }

        void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
        {
            LinqDataSource1.Select = null;
            // Make start date/time
            TextBox startDate = (TextBox)FormView1.FindControl("txtStartDate");
            NumericDropDownList hrs = (NumericDropDownList)FormView1.FindControl("nddStartHours");
            NumericDropDownList mins = (NumericDropDownList)FormView1.FindControl("nddStartMins");
            DateTime start = Convert.ToDateTime(string.Format("{0} {1}:{2}", startDate.Text, hrs.SelectedValue, mins.SelectedValue));
            //start.Add(new TimeSpan(int.Parse(hrs.SelectedValue), int.Parse(mins.SelectedValue), 0));

            TextBox endDate = (TextBox)FormView1.FindControl("txtEndDate");
            hrs = (NumericDropDownList)FormView1.FindControl("nddEndHours");
            mins = (NumericDropDownList)FormView1.FindControl("nddEndMins");
            DateTime end = Convert.ToDateTime(string.Format("{0} {1}:{2}", endDate.Text, hrs.SelectedValue, mins.SelectedValue));

            TextBox closeSignDate = (TextBox)FormView1.FindControl("txtSignUpCloseDate");
            hrs = (NumericDropDownList)FormView1.FindControl("nddSignUpCloseHours");
            DateTime closeSignUp = Convert.ToDateTime(string.Format("{0} {1}:00", closeSignDate.Text, hrs.SelectedValue, mins.SelectedValue));

            e.NewValues["StartDate"] = start;
            e.NewValues["EndDate"] = end;
            e.NewValues["SignUpCloseDate"] = closeSignUp;

        }

        void LinqDataSource1_Updated(object sender, LinqDataSourceStatusEventArgs e)
        {
            if (e.Exception == null)
            {
                scDataContext.Instance.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _event);
                this.SetTopMostMessage("活动内容已更新。", true);

                RadioButtonList tmp = (RadioButtonList)FormView1.FindControl("rdoPostEditAction");
                if (tmp.SelectedValue == "1")
                {
                    RefreshPage(true);
                }
                else
                {
                    Response.Redirect("MyEvents.aspx");
                }
            }
        }

        void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (!IsPostBack)
            {
                e.Result = _event;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }
    }
}
